<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/context/mytags.jsp"%>
<!DOCTYPE html>
<html>
 <head>
  <title>选择教师</title>
  <t:base type="jquery,easyui,tools,DatePicker"></t:base>
 </head>
 
 <body>
	<input type="hidden" value="${obj.id }" name="id" id="id">
	
	<div style="padding-top:5px;height: 300px;">
		<a class='myBtn roundBorder' onclick="selectData()">添加指导老师</a>
		<table class="commonTable" id='dataTable'>
			<thead>
				<tr>
					<th>工号</th>
					<th>姓名</th>
					<th>操作</th>
				</tr>
			</thead> 
			<tbody>
	 			<c:forEach items="${rows}" var="row">
	 				<tr id="row_${row.id}" name="row">
		 				<td>${row.staffNo }</td>
		 				<td>${row.realName }</td>
		 				<td>
							<a href='javascript:void(0)' onclick='dataTable.deleteRow("${row.id}")'>删除</a>
						</td>
 					</tr>
	 			</c:forEach>
			</tbody>
		</table> 
	</div>
	<div style="text-align: center;">
		<button onclick="save()">保存</button>
	</div>
 </body>  	
</div>
 <script>
 
 	$(function(){
 		dataTable.initParams();
 	});
	
	 function save(){
		 dataTable.setAddAndDelIds();
		 var url = 'labProjectController.do?doTeacherAdd';
		 $.ajax({
			 url: url,
			 type: 'post',
			 dataType: 'json',
			 data: {'addIds':dataTable.addIds.toString(), 'delIds':dataTable.delIds.toString(), 'id':$("#id").val()},
			 success: function(data){
				 if(data.success){
					 tip(data.msg);
					 location.reload();
				 }else{
					 tip(data.msg);
				 }
			 }
		 });
	 }
	 
	 function selectData(){
		 $.dialog({
			  	id:'xxx',
				content: 'url:teacherController.do?goSelect',
				lock : true,
				zIndex: getzIndex(), 
				parent:frameElement.api,
				width:480,
				height:350,
				title:'选择老师', 
				opacity : 0.3,
				cache:false,
			    ok: function(){
			    	iframe = this.iframe.contentWindow;
			    	var rows = iframe.getData();
			    	if(rows == null) return false;
			    	dataTable.addRows(rows);
			    },
			    cancelVal: '关闭',
			    cancel: true /*为true等价于function(){}*/
		});
	 }
	 
	 var dataTable = {
		savedIds : [],//原来保存的teachItem的id
		idsInTable: [],//目前所有在表格的id，包括原来从后台传来的
		addIds: [],//与原来的对比，添加的id
		delIds:[],//与原来的对比，被删除的id
		
		initParams: function(){
			var rows = $("[name='row']");
			for(var i=0;i<rows.length;i++){
				var id = rows[i].id.split('_');
				dataTable.savedIds.push(id[1]+'');
				dataTable.idsInTable.push(id[1]+'');
			}
		},
		deleteRow: function (id){
			$("#row_"+id).remove();
			var index = dataTable.idsInTable.indexOf(''+id);
			if(index!=-1){
				dataTable.idsInTable.splice(index,1);
			}
		},
		addRows:function(rows){
			for(var i=0;i<rows.length;i++){
				var row = rows[i];
				var id = row.id;
				
				if(dataTable.isInCollection(id, dataTable.idsInTable)!=-1){
					continue;
				}
				
				dataTable.idsInTable.push(''+id);
				
				$('#dataTable tbody').append("<tr id='row_"+id+"'></tr>");
				
				var html = '';
				html += 
					'<td>'+row.staffNo+'</td>'+
					'<td>'+ row.realName +'</td>'+
					'<td>'+
						'<a href="javascript:void(0)" onclick="dataTable.deleteRow('+ row.id +')">删除</a>'+
					'</td>';
					
				//添加一行
				$('#row_'+id).append(html);
			}
		},
		isInCollection: function (id, collection){
			for(var i =0;i<collection.length;i++){
				if(collection[i] == id){
					return i;
				}
			}
			return -1;
		},
		setAddAndDelIds: function(){
			for(var i=0;i<dataTable.savedIds.length;i++){
				var index = dataTable.idsInTable.indexOf(dataTable.savedIds[i]);
				if(index==-1){
					dataTable.delIds.push(dataTable.savedIds[i]);
				}
			}
			
			for(var i=0;i<dataTable.idsInTable.length;i++){
				var index = dataTable.savedIds.indexOf(dataTable.idsInTable[i]);
				if(index==-1){
					dataTable.addIds.push(dataTable.idsInTable[i]);
				}
			}
		}
  	};
 </script>
